﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimatorNS;

namespace LittleLove.UC
{
    public partial class SlideShowUC : UserControl
    {
        #region Attributs and Getter/Setter
        public Base.PresentDB.Events Event { get; set; }
        public List<Base.Slide> ListSlide { get; set; }
        private string m_description;
        private int m_indexDescription = 0;
        private int m_indexSlide = 0;
        #endregion

        public SlideShowUC()
        {
            InitializeComponent();
        }

        private void SlideShowUC_Load(object sender, EventArgs e)
        {
            ListSlide = Base.PresentDB.getEvent(Event);
            // Empty event
            if (ListSlide.Count == 0)
            {
                lblMsg1.Visible = lblMsg2.Visible = true;
                lblMsg1.BackColor = lblMsg2.BackColor = Color.Transparent;
            }
            timerAnimation.Start();
        }

        #region Other functions
        /// <summary>
        /// Release timers
        /// </summary>
        public void releaseSlideShow()
        {
            timerAnimation.Stop();
            timerAnimation.Dispose();
            timerDescription.Stop();
            timerDescription.Dispose();
        }

        #endregion

        #region Animator logic functions
        /// <summary>
        /// Hide image
        /// </summary>
        private void hideControls()
        {
            animator.Hide(this);
            animator.WaitAllAnimations();
        }

        /// <summary>
        /// Create animation for image
        /// </summary>
        private void createAnimation()
        {
            animator.DefaultAnimation = LittleLove.Base.AnimatorBase.getRandomAnimation();
        }

        /// <summary>
        /// Show display image
        /// </summary>
        private void showControls()
        {
            animator.Show(this);
            animator.WaitAllAnimations();
        }

        /// <summary>
        /// Perform animation for image
        /// </summary>
        private void doAnimation()
        {
            // Reset description text
            lblDescription.Text = "";

            // Wait all animations will be completed
            animator.WaitAllAnimations();
            hideControls();
            // Set new image
            this.BackgroundImage = ListSlide[m_indexSlide].ImageView;
            createAnimation();
            showControls();

            // Start description timer
            timerDescription.Start();
            // Change speed of timer according to length of description
            timerDescription.Interval = (int)600 / ListSlide[m_indexSlide].Description.Length;
        }
        #endregion

        #region Events
        private void animator_TransfromNeeded(object sender, TransfromNeededEventArg e)
        {
            e.UseDefaultMatrix = true;
        }

        private void timerAnimation_Tick(object sender, EventArgs e)
        {
            // To avoid case Slide Show form is closed when timer is running
            try
            {
                if (m_indexSlide < ListSlide.Count)
                {
                    doAnimation();
                    // To avoid case when m_indexSlide increase before description is printed
                    m_description = ListSlide[m_indexSlide].Description;
                    m_indexSlide++; // Next image
                }
                else
                {
                    m_indexSlide = 0; // Reset to first image
                }
            }
            catch (Exception)
            {
                timerAnimation.Stop();
                return;
            }

        }

        private void timerDescription_Tick(object sender, EventArgs e)
        {
            // To avoid case Slide Show form is closed when timer is running
            try
            {
                if (m_indexDescription < m_description.Length)
                {
                    lblDescription.Text +=
                        m_description[m_indexDescription].ToString();
                    m_indexDescription++; // Next charactor
                }
                else
                {
                    m_indexDescription = 0; // Reset to first charactor
                    timerDescription.Stop(); // Stop printing description
                }
            }
            catch (Exception)
            {
                timerDescription.Stop();
                return;
            }
        }
        #endregion
    }
}
